package com.hsteach.web.controller.rota;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.hsteach.common.annotation.Anonymous;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.dao.domain.rota.RotaFiles;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.service.rota.RotaRecordService;
import com.hsteach.web.controller.rota.dto.AddRotaRecordDto;
import com.hsteach.web.controller.rota.dto.ListRotaRecordDto;
import com.hsteach.web.controller.rota.vo.ListRotaRecordExcel;
import com.hsteach.web.controller.rota.vo.ListRotaRecordVo;
import com.hsteach.web.controller.rota.vo.MineRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 值班记录Controller
 * 
 * @author fanxquer
 * @date 2023-10-22
 */
@Api(value = "值班记录管理", tags = "值班记录管理")
@RestController
@RequestMapping("/rota/record")
public class RotaRecordController extends BaseController
{
    @Autowired
    private RotaRecordService rotaRecordService;

    /**
     * 查询值班记录列表
     */
    @ApiOperation("查询值班记录列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ListRotaRecordDto dto)
    {
        List<ListRotaRecordVo> listRotaRecordVos = rotaRecordService.listRecord(dto);
        return getDataTable(listRotaRecordVos);
    }

    /**
     * 查询值班记录填写列表
     */
    @ApiOperation("查询我的值班记录填写列表")
    @PostMapping("/listMine")
    public TableDataInfo listMine(@RequestBody ListRotaRecordDto dto)
    {
        List<MineRecordVo> mineRecordVos = rotaRecordService.listMine(dto);
        return getDataTable(mineRecordVos);
    }

    /**
     * 导出值班记录列表
     */
    @ApiOperation("导出值班记录列表")
    @GetMapping("/export")
    @Anonymous
    public void export(HttpServletResponse response, ListRotaRecordDto dto)
    {
        List<ListRotaRecordVo> list = rotaRecordService.listRecord(dto);
        List<ListRotaRecordExcel> rows = list.stream().flatMap(e -> {
            return e.getRecords().stream().filter(k -> BooleanEnum.TRUE.getCode().equals(k.getRecordFlg())).map(j -> {
                ListRotaRecordExcel excel = new ListRotaRecordExcel();
                excel.setRecordTime(e.getDutyDate());
                excel.setPersonName(j.getPersonName());
                excel.setRecordContent(j.getRecordContent());
                excel.setFiles(j.getFiles().stream().map(RotaFiles::getUrl).collect(Collectors.joining("\r\n")));
                return excel;
            });
        }).collect(Collectors.toList());
//        }).map(e -> {
//            ListRotaRecordExcel excel = new ListRotaRecordExcel();
//            excel.setRecordTime(e.getRecordTime());
//            excel.setPersonName(e.getPersonName());
//            excel.setRecordContent(e.getRecordContent());
//            excel.setFiles(e.getFiles().stream().map(RotaFiles::getUrl).collect(Collectors.joining("\r\n")));
//            return excel;
//        }).collect(Collectors.toList());
        ExcelUtil<ListRotaRecordExcel> util = new ExcelUtil<>(ListRotaRecordExcel.class);
        util.exportExcel(response, rows, "值班记录");
    }

    /**
     * 获取值班记录详细
     */
    @ApiOperation("获取某天值班记录填写详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rotaRecordService.selectRotaRecordById(id));
    }

    /**
     * 填写值班记录
     */
    @ApiOperation("填写值班记录")
    @PostMapping
    public AjaxResult add(@RequestBody AddRotaRecordDto dto)
    {
        return toAjax(rotaRecordService.add(dto));
    }

    /**
     * 修改值班记录
     */
    @ApiOperation("修改值班记录")
    @PutMapping
    public AjaxResult edit(@RequestBody AddRotaRecordDto dto)
    {
        return toAjax(rotaRecordService.update(dto));
    }
}
